﻿Public Class XFormParticipantes 

    Private Sub XFormParticipantes_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Cargar()

        If EventoActivo.IDEvento = 0 Then
            btnAgregar.Enabled = False
        Else
            btnAgregar.Enabled = True
        End If
    End Sub

    Sub Cargar()
        CarrerasTableAdapter.Fill(DsEvento.Carreras)
        ParticipantesMujeresTableAdapter.Fill(DsEvento.ParticipantesMujeres, EventoActivo.IDEvento)
        ParticipantesHombresTableAdapter.Fill(DsEvento.ParticipantesHombres, EventoActivo.IDEvento)

        Using DT1 As DataTable = ParticipantesMujeresTableAdapter.GetData(EventoActivo.IDEvento)
            If DT1.Rows.Count = 0 Then
                GridControl1.Enabled = False
            Else
                GridControl1.Enabled = True
            End If
        End Using

        Using DT2 As DataTable = ParticipantesHombresTableAdapter.GetData(EventoActivo.IDEvento)
            If DT2.Rows.Count = 0 Then
                GridControl2.Enabled = False
            Else
                GridControl2.Enabled = True
            End If
        End Using
    End Sub

    Private Structure IDS
        Dim IDM As Integer
        Dim IDH As Integer
        Dim NombreM As String
        Dim NombreH As String
    End Structure

    Private Function DameIDS() As IDS
        Try
            Dim ids As IDS = New IDS() With {
                .IDM = GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "IDParticipante"),
                .IDH = GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "IDParticipante"),
                .NombreM = String.Format("{0} {1}", GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "Nombre"), GridView1.GetRowCellValue(GridView1.FocusedRowHandle, "Apellido")),
                .NombreH = String.Format("{0} {1}", GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Nombre"), GridView2.GetRowCellValue(GridView2.FocusedRowHandle, "Apellido"))
            }
            Return ids
        Catch ex As Exception
            Return Nothing
        End Try
    End Function


    Private Sub btnAgregar_Click(sender As System.Object, e As System.EventArgs) Handles btnAgregar.Click
        With XFormAgregarParticipante
            .MdiParent = Me.MdiParent
            .EsNuevo1 = True
            .Show()
            .Focus()
        End With
    End Sub

    Private Sub EditarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles EditarToolStripMenuItem.Click
        Dim item As ToolStripMenuItem = CType(sender, ToolStripMenuItem) 'Se obtiene el item clickeado
        Dim menu As ContextMenuStrip = CType(item.Owner, ContextMenuStrip) 'Se obtiene el menú contextual donde está el item

        Dim ID As Integer

        Select Case menu.SourceControl.Name 'Se obtiene el nombre del control que tiene configurado el menú contextual
            Case "GridControl1"
                ID = DameIDS.IDM
            Case "GridControl2"
                ID = DameIDS.IDH
        End Select

        With XFormAgregarParticipante
            .IDP1 = ID
            .MdiParent = Me.MdiParent
            .EsNuevo1 = False
            .Show()
            .Focus()
        End With
    End Sub

    Private Sub VerEvaluaciónToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles VerEvaluaciónToolStripMenuItem.Click
        Dim item As ToolStripMenuItem = CType(sender, ToolStripMenuItem) 'Se obtiene el item clickeado
        Dim menu As ContextMenuStrip = CType(item.Owner, ContextMenuStrip) 'Se obtiene el menú contextual donde está el item

        Dim ID As Integer
        Dim Nombre As String

        Select Case menu.SourceControl.Name 'Se obtiene el nombre del control que tiene configurado el menú contextual
            Case "GridControl1"
                ID = DameIDS.IDM
                Nombre = DameIDS.NombreM
            Case "GridControl2"
                ID = DameIDS.IDH
                Nombre = DameIDS.NombreH
        End Select

        With XFormDialogResultados
            .IDParticipante1 = ID
            .Nombre1 = Nombre
            .ShowDialog()
        End With
    End Sub
End Class